/*
 * Copyright © 2009 Perseus Project - Tufts University <http://www.perseus.tufts.edu>
 *
 * This file is part of MorphAdjustPerseus.
 *
 * MorphAdjustPerseus is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * MorphAdjustPerseus is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with MorphAdjustPerseus.  If not, see <http://www.gnu.org/licenses/>.
 */

package eu.himeros.text;

import eu.himeros.transcoder.Transcoder;
import eu.himeros.util.IoManager;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.util.Hashtable;

/**
 * Add morphs that are not present in the Morpheus list. 
 * E.g. <g enc="betacode">kai\</g> added to <g enc="betacode">kai/</g>
 *
 * @author Federico Boschetti <federico.boschetti.73@gmail.com>
 */
public class MorphListCompletion {
    Transcoder trans=null;
    BufferedReader br=null;
    BufferedReader vowelsBr=null;
    BufferedWriter bw=null;
    String category=null;
    Hashtable<String,String> vowelsHt=new Hashtable<String, String>(250);

    /**
     * Default Constructor.
     */
    public MorphListCompletion(){}

    /**
     * Constructor that receive the following arguments: <br/>
     * <code>arg[0]</code> acute to grave transoding file;<br/>
     * <code>arg[1]</code> morph list input file;<br/>
     * <code>arg[2]</code> vowels file;<br/>
     * <code>arg[3]</code> output file;<br/>
     * @param args the argument string array.
     */
    public MorphListCompletion(String[] args){
        init(args);
        parse();
    }

    /**
     * Init the class.
     * @param args the argument string array.
     */
    protected void init(String[] args){
        trans=new Transcoder(args[0]); //acu2grave.txt
        br=IoManager.getReader(args[1]);
        vowelsBr=IoManager.getReader(args[2]); //vowels.txt
        bw=IoManager.getWriter(args[3]);
        try{
            String line=null;
            while((line=vowelsBr.readLine())!=null){
                if(line.startsWith("//")) continue;
                if(line.startsWith("#")){
                    category=line.substring(1);
                    continue;
                }
                vowelsHt.put(line, category);
            }
        }catch(Exception ex){ex.printStackTrace();}
    }

    /**
     * Parse the words and add missing morphs.
     */
    public void parse(){
        String line=null;
        StringBuffer lineSb=null;
        int vowelCnt=0; // records how many vowels [for diphthongs: only second element] are present in the string
        char currChar=0;
        int accentType=0;
        int vowelType=0;
        int nextVowelType=0;
        String nextCategory=null;
        int[] vowelPoss=new int[50]; //position of vowels that can bear the accent
        int[] accentTypes=new int[50];
        try{
            while((line=br.readLine())!=null){
                lineSb=new StringBuffer(line);
                vowelCnt=-1;
                for(int i=0;i<line.length();i++){
                    currChar=line.charAt(i);
                    category=vowelsHt.get(""+currChar);
                    if(category==null) continue;
                    nextCategory=(i<line.length()-1)?vowelsHt.get(""+line.charAt(i+1)):null;
                    vowelType=Integer.parseInt(""+category.charAt(0));
                    accentType=Integer.parseInt(""+category.charAt(1));
                    nextVowelType=(nextCategory!=null)?nextCategory.charAt(0):-1;
                    if(vowelType==0&&nextVowelType>1) continue;
                    vowelCnt++;
                    vowelPoss[vowelCnt]=i;
                    accentTypes[vowelCnt]=accentType;
                    
                }
                
                if((vowelCnt>-1&&accentTypes[vowelCnt]==1)||(vowelCnt>0&&accentTypes[vowelCnt-1]==2)||(vowelCnt>1&&accentTypes[vowelCnt-2]==1)){
                    lineSb.setCharAt(vowelPoss[vowelCnt],trans.parse(""+lineSb.charAt(vowelPoss[vowelCnt])).charAt(0));
                }
                bw.write(line);bw.newLine();
                if(!line.equals(lineSb.toString())){
                    //System.out.println(line+" : "+lineSb.toString());
                    bw.write(lineSb.toString());bw.newLine();
                }
            }
        }catch(Exception ex){ex.printStackTrace();}
    }

    /**
     * The <code>main</code> method.
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new MorphListCompletion(args);
    }

}